---
title: Cohere Rerank
---

Reranking documents can greatly improve any RAG application and document retrieval system.

At a high level, a rerank API is a language model which analyzes documents and reorders them based on their relevance to a given query.

Cohere offers an API for reranking documents. In this example we'll show you how to use it.

## Setup

import IntegrationInstallTooltip from '/snippets/javascript-integrations/integration-install-tooltip.mdx';

<IntegrationInstallTooltip/>

```bash npm
npm install @langchain/cohere @langchain/core
```

import CohereRerank from "/snippets/javascript-integrations/examples/document_compressors/cohere_rerank.mdx";

<CohereRerank />

Here, we can see the `.rerank()` method returns just the index of the documents (matching the indexes of the input documents) and their relevancy scores.

If we'd like to have the documents returned from the method itself, we can use the `.compressDocuments()` method.

import CohereRerankCompressor from "/snippets/javascript-integrations/examples/document_compressors/cohere_rerank_compressor.mdx";

<CohereRerankCompressor />

From the results, we can see it returned the top 3 documents, and assigned a `relevanceScore` to each.

As expected, the document with the highest `relevanceScore` is the one that references Washington, D.C., with a score of `98.7%`!

### Usage with `CohereClient`

If you are using Cohere on Azure, AWS Bedrock or a standalone instance you can use the `CohereClient` to create a `CohereRerank` instance with your endpoint.

import CohereRerankCustomClient from "/snippets/javascript-integrations/examples/document_compressors/cohere_rerank_custom_client.mdx";

<CohereRerankCustomClient />
